﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using InSafetyAirClient.InSafetyAirService;
using System.Diagnostics;

namespace InSafetyAirClient.GUI
{
    public partial class Create : UserControl
    {
        private Label[] labels = null;
        private DataGridView dataGridView1;
        private DataGridView dataGridView2;
        DataTable dataTable1;
        DataTable dataTable2;
        private TextBox[] textBoxes = null;
        private MonthCalendar startCalendar;
        private MonthCalendar endCalendar;
        private Label startDateLabel;
        private Label endDateLabel;
        private TextBox priceTextBox;
        private ComboBox[] comboxBoxes;
        private Button[] buttons = null;
        private CheckBox repeatWeely;
        private DateTime startDate;
        private DateTime endDate;
        private NumericUpDown numericUpDownHour1;
        private NumericUpDown numericUpDownHour2;
        private NumericUpDown numericUpDownMinute1;
        private NumericUpDown numericUpDownMinute2;
        public ServiceClient serv = null;

        public Create(int x)
        {
            InitializeComponent();
            switch (x)
            {
                case 1:
                    {
                        createFlightAndBuddies(x);
                    }
                    break;
            }
        }

        private void createFlightAndBuddies(int x)
        {
            buttons= new Button[4];
            string[] createFlight = new string[] { "Airline", "Plane", "Segment(s)", "Startairport", "Endairport", "Price", "Schedule(s)", "StartDate", "EndDate" };
            labels = new Label[createFlight.Length];
            comboxBoxes = new ComboBox[5];
            int jump=50;
            for (int i = 0; i < createFlight.Length; i++)
            {
                labels[i] = new Label();
                labels[i].AutoSize = true;
                jump +=35;
                labels[i].Location = new System.Drawing.Point(20, jump);
                if (i == 2)
                    jump += 100;
                else if (i == 6)
                    jump += 110;
                labels[i].Font = new System.Drawing.Font("Times New Roman", 20.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                labels[i].Name = createFlight[i] + "Label";
                labels[i].Size = new System.Drawing.Size(35, 13);
                labels[i].TabIndex = i;
                labels[i].Text = createFlight[i] + ": ";
                this.Controls.Add(labels[i]);
            }
            jump = 55;
            for (int i = 0; i < 4; i++)
            {
                comboxBoxes[i] = new System.Windows.Forms.ComboBox(); 
                //comboxBoxes[i].FormattingEnabled = true;
                jump += 35;
                comboxBoxes[i].Location = new System.Drawing.Point(180, jump);
                if (i==1)
                {
                    jump += 135;
                }
                comboxBoxes[i].Name = "comboBox1";
                comboxBoxes[i].Size = new System.Drawing.Size(121, 21);
                comboxBoxes[i].TabIndex = 8;
                this.Controls.Add(comboxBoxes[i]);
            }
            serv = new ServiceClient();
            AutoCompleteStringCollection airportNames = new AutoCompleteStringCollection();
            List<Airport> airports = serv.GetAll(typeof(Airport).Name).OfType<Airport>().ToList();
            List<string> airpname=new List<string>();
            foreach (Airport airpot in airports)
            {
                airportNames.Add(airpot.name);
                comboxBoxes[2].Items.Add(airpot.name);
                comboxBoxes[3].Items.Add(airpot.name);
            }

            comboxBoxes[2].AutoCompleteMode = AutoCompleteMode.Suggest;
            comboxBoxes[2].AutoCompleteSource = AutoCompleteSource.CustomSource;
            comboxBoxes[2].AutoCompleteCustomSource = airportNames;
            comboxBoxes[2].DropDownStyle = ComboBoxStyle.DropDown;
            comboxBoxes[3].AutoCompleteMode = AutoCompleteMode.Suggest;
            comboxBoxes[3].AutoCompleteSource = AutoCompleteSource.CustomSource;
            comboxBoxes[3].AutoCompleteCustomSource = airportNames;
            comboxBoxes[3].DropDownStyle = ComboBoxStyle.DropDown;

            //
            //Text
            //
            jump += 35;
            priceTextBox = new TextBox();
            priceTextBox.Location = new System.Drawing.Point(180, jump);
            priceTextBox.Name = "priceTextBox";
            priceTextBox.Size = new System.Drawing.Size(100, 20);
            priceTextBox.TabIndex = 0;
            this.Controls.Add(priceTextBox);
            // 
            // dataGridView1
            // 
            dataGridView1 = new DataGridView();
            dataGridView1.Size = new System.Drawing.Size(400, 90);
            dataGridView1.Location = new System.Drawing.Point(50, 195);
            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.AllowUserToDeleteRows = false;
            dataGridView1.ReadOnly = true;
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dataTable1 = new DataTable();
            dataTable1.Columns.Add("coloana 1", typeof(string));
            dataTable1.Columns.Add("coloana 2", typeof(string));
            dataTable1.Columns.Add("coloana 3", typeof(string));

            dataTable1.Rows.Add("test1", "test2", "test3");
            dataTable1.Rows.Add("test1", "test2", "test3");
            dataTable1.Rows.Add("test1", "test2", "test3");
            dataGridView1.DataSource = dataTable1;
            this.Controls.Add(dataGridView1);
            //
            //addSegmentButton
            //
            buttons[1] = new Button();
            buttons[1].Location = new System.Drawing.Point(300, 400);
            buttons[1].Name = "addSegment";
            buttons[1].Size = new System.Drawing.Size(150, 23);
            buttons[1].TabIndex = 0;
            buttons[1].MouseClick += new MouseEventHandler(addSegment);
            buttons[1].Text = "Add Segment";
            buttons[1].UseVisualStyleBackColor = true;
            this.Controls.Add(buttons[1]);
            #region Shedule
            // 
            // dataGridView2
            // 
            dataGridView2 = new DataGridView();
            dataGridView2.Size = new System.Drawing.Size(400, 90);
            dataGridView2.Location = new System.Drawing.Point(50, 435);
            dataGridView2.AllowUserToAddRows = false;
            dataGridView2.AllowUserToDeleteRows = false;
            dataGridView2.ReadOnly = true;
            dataGridView2.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dataTable2 = new DataTable();
            dataTable2.Columns.Add("Departure", typeof(string));
            dataTable2.Columns.Add("Arrival", typeof(string));
            dataTable2.Columns.Add("Weekly", typeof(string));
            dataGridView2.DataSource = dataTable2;
            this.Controls.Add(dataGridView2);
            //
            //startDateLabel
            //
            jump += 180;
            startDateLabel= new Label();
            startDateLabel.Location = new System.Drawing.Point(180, jump);
            startDateLabel.Name = "startDate";
            startDateLabel.Size = new System.Drawing.Size(60, 20);
            startDateLabel.TabIndex = 0;
            startDateLabel.AutoSize = false;
            startDateLabel.BackColor = System.Drawing.Color.White;
            startDateLabel.MouseClick += new MouseEventHandler(showCallandar1);
            this.Controls.Add(startDateLabel);
            //
            //callendar 1
            //
            startCalendar = new System.Windows.Forms.MonthCalendar();           
            startCalendar.MaxSelectionCount = 1;
            startCalendar.Location = new System.Drawing.Point(185, jump-200);
            startCalendar.Name = "monthCalendar1";
            startCalendar.TabIndex = 0;
            startCalendar.Visible=false;
            startCalendar.DateChanged += new DateRangeEventHandler(startCalendarChange);
            Controls.Add(startCalendar);
            startCalendar.BringToFront();
            //
            //endDateLabel
            //
            jump += 35;
            endDateLabel = new Label();
            endDateLabel.Location = new System.Drawing.Point(180, jump);
            endDateLabel.Name = "endDate";
            endDateLabel.AutoSize = false;
            endDateLabel.BackColor = System.Drawing.Color.White;
            endDateLabel.Size = new System.Drawing.Size(60, 20);
            endDateLabel.MouseClick += new MouseEventHandler(showCallandar2);
            endDateLabel.TabIndex = 0;
            this.Controls.Add(endDateLabel);
            //
            //callendar 2
            //
            endCalendar = new System.Windows.Forms.MonthCalendar();
            endCalendar.MaxSelectionCount = 1;
            endCalendar.Location = new System.Drawing.Point(185, jump - 200);
            endCalendar.Name = "monthCalendar1";
            endCalendar.TabIndex = 0;
            endCalendar.Visible = false;
            endCalendar.DateChanged += new DateRangeEventHandler(endCalendarChange);
            Controls.Add(endCalendar);
            endCalendar.BringToFront();
            //
            //repeatsWeakly?
            //
            repeatWeely= new CheckBox();
            repeatWeely.Location =new System.Drawing.Point(345, jump+2);
            repeatWeely.Text = "Repeats weakly?";
            repeatWeely.AutoSize = true;
            repeatWeely.Size = new System.Drawing.Size(80, 17);
            repeatWeely.TabIndex = 0;
            repeatWeely.UseVisualStyleBackColor = true;
            this.Controls.Add(repeatWeely);
            // 
            // addSheduleButton
            // 
            buttons[0] = new Button();
            buttons[0].Location = new System.Drawing.Point(200, jump + 35);
            buttons[0].Name = "addShedule";
            buttons[0].Size = new System.Drawing.Size(150, 23);
            buttons[0].TabIndex = 0;
            buttons[0].MouseClick += new MouseEventHandler(addShedule);
            buttons[0].Text = "Add Schedule";
            buttons[0].UseVisualStyleBackColor = true;
            this.Controls.Add(buttons[0]);
            //
            //numericupdownHour1
            //
            numericUpDownHour1 = new System.Windows.Forms.NumericUpDown();
            numericUpDownHour1.Location = new System.Drawing.Point(245, jump - 35);
            numericUpDownHour1.Maximum = new decimal(new int[] { 23, 0, 0, 0 });
            numericUpDownHour1.Name = "numericUpDown1";
            numericUpDownHour1.Size = new System.Drawing.Size(39, 20);
            numericUpDownHour1.TabIndex = 0;
            Controls.Add(this.numericUpDownHour1);
            //
            //numericupdownHour2
            //
            numericUpDownHour2 = new System.Windows.Forms.NumericUpDown();
            numericUpDownHour2.Location = new System.Drawing.Point(245, jump);
            numericUpDownHour2.Maximum = new decimal(new int[] { 23, 0, 0, 0 });
            numericUpDownHour2.Name = "numericUpDown1";
            numericUpDownHour2.Size = new System.Drawing.Size(39, 20);
            numericUpDownHour2.TabIndex = 0;
            Controls.Add(this.numericUpDownHour2);
            //
            //numericupdownMinute1
            //
            numericUpDownMinute1 = new System.Windows.Forms.NumericUpDown();
            numericUpDownMinute1.Location = new System.Drawing.Point(285, jump - 35);
            numericUpDownMinute1.Maximum = new decimal(new int[] { 59, 0, 0, 0 });
            numericUpDownMinute1.Name = "numericUpDown1";
            numericUpDownMinute1.Size = new System.Drawing.Size(39, 20);
            numericUpDownMinute1.TabIndex = 0;
            Controls.Add(this.numericUpDownMinute1);
            //
            //numericupdownMinute2
            //
            numericUpDownMinute2 = new System.Windows.Forms.NumericUpDown();
            numericUpDownMinute2.Location = new System.Drawing.Point(285, jump);
            numericUpDownMinute2.Maximum = new decimal(new int[] { 59, 0, 0, 0 });
            numericUpDownMinute2.Name = "numericUpDown1";
            numericUpDownMinute2.Size = new System.Drawing.Size(39, 20);
            numericUpDownHour2.TabIndex = 0;
            Controls.Add(this.numericUpDownMinute2);
            #endregion 
        }

        private void showCallandar1(object sender, EventArgs e)
        {
            if (endCalendar.Visible)
                endCalendar.Hide();
            if (startCalendar.Visible)
                startCalendar.Hide();
            else
                startCalendar.Show();
        }

        private void showCallandar2(object sender, EventArgs e)
        {
            if (startCalendar.Visible)
                startCalendar.Hide();
            if (endCalendar.Visible)
                endCalendar.Hide();
            else
                endCalendar.Show();
        }

        private void startCalendarChange(object sender, DateRangeEventArgs e)
        {
            startDate = startCalendar.SelectionRange.Start;
            startDateLabel.Text = startCalendar.SelectionRange.Start.ToString();
        }

        private void endCalendarChange(object sender, DateRangeEventArgs e)
        {
            endDate = endCalendar.SelectionRange.Start;
            endDateLabel.Text = endCalendar.SelectionRange.Start.ToString();
        }

        private void addShedule(object sender, EventArgs e)
        {
            if (endDateLabel.Text != "" && startDateLabel.Text != "")
            {
                startDate = startDate.AddHours(System.Convert.ToDouble(numericUpDownHour1.Value));
                startDate = startDate.AddMinutes(System.Convert.ToDouble(numericUpDownMinute1.Value));
                endDate = endDate.AddHours(System.Convert.ToDouble(numericUpDownHour2.Value));
                endDate = endDate.AddMinutes(System.Convert.ToDouble(numericUpDownMinute2.Value));
                if (endDate > startDate)
                {
                    if (repeatWeely.Checked)
                        dataTable2.Rows.Add(startDate.ToString(), endDate.ToString(), "Yes");
                    else
                        dataTable2.Rows.Add(startDate.ToString(), endDate.ToString(), "No");
                }
                else
                    Console.Beep();
            }
        }

        private void addSegment(object sender, EventArgs e)
        {
        }

    }
}
